package controller;

import pojo.User;
import service.UserManagement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author: zen
 * @date: 2020/4/19
 * @Title: SecretChange
 * @Description:
 */

@WebServlet("/secretChange")
public class SecretChange extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        PrintWriter out = resp.getWriter();
        User user = (User) session.getAttribute("user");
        // 使用卫语句
        if (user == null) {
            System.out.println("用户未登录，无法修改密码");
            out.write("error");
            out.flush();
            out.close();
            return;
        }
        String pass = req.getParameter("pass");
        user.setPass(pass);
        if (UserManagement.adaptSecret(user)) {
            out.write("ok");
            System.out.println("用户修改密码：ok");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
